package com.njtoyo.taxi.admin.service.data.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.njtoyo.taxi.admin.library.Const;
import com.njtoyo.taxi.admin.library.common.util.ChartUtil;
import com.njtoyo.taxi.admin.library.common.util.CopyUtil;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.cache.*;
import com.njtoyo.taxi.admin.rest.presenter.common.ChartCyclesTimeListPresenter;
import com.njtoyo.taxi.admin.rest.presenter.data.allowance.*;
import com.njtoyo.taxi.admin.rest.wrapper.data.allowance.AllowanceChartWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.data.allowance.AllowanceWrapper;
import com.njtoyo.taxi.admin.service.data.AllowanceCustomerService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("AllowanceCustomerServiceCacheImpl")
@DS(Const.SECONDARY_DS)
public class AllowanceCustomerServiceCacheImpl implements AllowanceCustomerService {

    @Autowired
    private CacheAllowanceCustomerWechatMapper cacheAllowanceCustomerWechatMapper;

    @Autowired
    private CacheAllowanceCustomerBalanceMapper cacheAllowanceCustomerBalanceMapper;

    @Autowired
    private CacheAllowanceCustomerCouponMapper cacheAllowanceCustomerCouponMapper;

    @Autowired
    private CacheAllowanceCustomerWaveMapper cacheAllowanceCustomerWaveMapper;

    @Autowired
    private CacheAllowanceCustomerProportionMapper cacheAllowanceCustomerProportionMapper;

    @Override
    public RestResult<CustomerProportionPresenter> proportion(AdminUser adminUser, AllowanceWrapper wrapper) {
        String startTime = DateUtil.formatDateTime(wrapper.getStartTime());
        String endTime = DateUtil.formatDateTime(wrapper.getEndTime());

        CustomerProportionPresenter res = cacheAllowanceCustomerProportionMapper.selectCache(startTime, endTime);

        return RestResult.success(res);
    }

    @Override
    public RestResult<CustomerCouponPresenter> coupon(AdminUser adminUser, AllowanceWrapper wrapper) {
        String startTime = DateUtil.formatDateTime(wrapper.getStartTime());
        String endTime = DateUtil.formatDateTime(wrapper.getEndTime());

        CustomerCouponPresenter res = cacheAllowanceCustomerCouponMapper.selectCache(startTime, endTime);

        return RestResult.success(res);
    }

    @Override
    public RestResult<CustomerWechatPresenter> wechat(AdminUser adminUser, AllowanceWrapper wrapper) {
        String startTime = DateUtil.formatDateTime(wrapper.getStartTime());
        String endTime = DateUtil.formatDateTime(wrapper.getEndTime());

        CustomerWechatPresenter res = cacheAllowanceCustomerWechatMapper.selectCache(startTime, endTime);

        return RestResult.success(res);
    }

    @Override
    public RestResult<CustomerWavePresenter> wave(AdminUser adminUser, AllowanceWrapper wrapper) {
        String startTime = DateUtil.formatDateTime(wrapper.getStartTime());
        String endTime = DateUtil.formatDateTime(wrapper.getEndTime());

        CustomerWavePresenter res = cacheAllowanceCustomerWaveMapper.selectCache(startTime, endTime);

        return RestResult.success(res);
    }

    @Override
    public RestResult<CustomerBalancePresenter> balance(AdminUser adminUser, AllowanceWrapper wrapper) {
        String startTime = DateUtil.formatDateTime(wrapper.getStartTime());
        String endTime = DateUtil.formatDateTime(wrapper.getEndTime());

        CustomerBalancePresenter res = cacheAllowanceCustomerBalanceMapper.selectCache(startTime, endTime);

        return RestResult.success(res);
    }

    @Override
    public RestResult<List<CustomerCouponChartPresenter>> couponChart(AdminUser adminUser, AllowanceChartWrapper wrapper) {
        List<CustomerCouponChartPresenter> data = cacheAllowanceCustomerCouponMapper.selectChartCache(wrapper);

        List<ChartCyclesTimeListPresenter> timeList = ChartUtil.getCyclesByCustomizeDaily(wrapper.getStartDate(), wrapper.getEndDate());
        List<CustomerCouponChartPresenter> timeListConv = CopyUtil.copyList(timeList, CustomerCouponChartPresenter.class);

        for (CustomerCouponChartPresenter timeItem : timeListConv) {
            for (CustomerCouponChartPresenter item : data) {
                if (timeItem.getStartTime().equals(item.getStartTime())) {
                    timeItem.setTotalValue(item.getTotalValue());
                    timeItem.setHadUsedValue(item.getHadUsedValue());
                }
            }
        }
        return RestResult.success(timeListConv);
    }

    @Override
    public RestResult<List<CustomerWaveChartPresenter>> waveChart(AdminUser adminUser, AllowanceChartWrapper wrapper) {
        List<CustomerWaveChartPresenter> data = cacheAllowanceCustomerWaveMapper.selectChartCache(wrapper);

        List<ChartCyclesTimeListPresenter> timeList = ChartUtil.getCyclesByCustomizeDaily(wrapper.getStartDate(), wrapper.getEndDate());
        List<CustomerWaveChartPresenter> timeListConv = CopyUtil.copyList(timeList, CustomerWaveChartPresenter.class);

        for (CustomerWaveChartPresenter timeItem : timeListConv) {
            for (CustomerWaveChartPresenter item : data) {
                if (timeItem.getStartTime().equals(item.getStartTime())) {
                    timeItem.setActualAmount(item.getActualAmount());
                }
            }
        }

        return RestResult.success(timeListConv);
    }

    @Override
    public RestResult<List<CustomerBalanceChartPresenter>> balanceChart(AdminUser adminUser, AllowanceChartWrapper wrapper) {
        List<CustomerBalanceChartPresenter> data = cacheAllowanceCustomerBalanceMapper.selectChartCache(wrapper);

        List<ChartCyclesTimeListPresenter> timeList = ChartUtil.getCyclesByCustomizeDaily(wrapper.getStartDate(), wrapper.getEndDate());
        List<CustomerBalanceChartPresenter> timeListConv = CopyUtil.copyList(timeList, CustomerBalanceChartPresenter.class);

        for (CustomerBalanceChartPresenter timeItem : timeListConv) {
            for (CustomerBalanceChartPresenter item : data) {
                if (timeItem.getStartTime().equals(item.getStartTime())) {
                    timeItem.setHadUsedValue(item.getHadUsedValue());
                    timeItem.setTotalValue(item.getTotalValue());
                }
            }
        }

        return RestResult.success(timeListConv);
    }

    @Override
    public RestResult<List<CustomerProportionChartPresenter>> proportionChart(AdminUser adminUser, AllowanceChartWrapper wrapper) {
        List<CustomerProportionChartPresenter> data = cacheAllowanceCustomerProportionMapper.selectChartCache(wrapper);

        List<ChartCyclesTimeListPresenter> timeList = ChartUtil.getCyclesByCustomizeDaily(wrapper.getStartDate(), wrapper.getEndDate());
        List<CustomerProportionChartPresenter> timeListConv = CopyUtil.copyList(timeList, CustomerProportionChartPresenter.class);

        for (CustomerProportionChartPresenter timeItem : timeListConv) {
            for (CustomerProportionChartPresenter item : data) {
                if (timeItem.getStartTime().equals(item.getStartTime())) {
                    timeItem.setCouponAmount(item.getCouponAmount());
                    timeItem.setAllowanceAmount(item.getAllowanceAmount());
                    timeItem.setBalanceAmount(item.getBalanceAmount());
                    timeItem.setWechatAmount(item.getWechatAmount());
                }
            }
        }

        return RestResult.success(timeListConv);
    }
}
